<template>
  <main>
    <div class="h">新增商品</div>
    <span class="title-x">基本信息</span>
    <span class="title-x title">销售信息</span>
    <span class="title-x introduce">商品介绍</span>

    <!-- 表单信息 -->
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <!-- 商品名称 -->
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <!-- 商品类型 -->
      <el-form-item label="商品类型" prop="type">
        <el-input v-model="ruleForm.type" />
      </el-form-item>
      <!-- 品牌   生产 -->
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="品牌" prop="brand">
            <el-input v-model="ruleForm.brand" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产地" prop="production">
            <el-input v-model="ruleForm.production" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 需量 规格 -->
      <el-row :gutter="15" class="spacing">
        <el-col :span="12">
          <el-form-item label="需量" prop="amount">
            <el-input v-model="ruleForm.amount" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格" prop="specs">
            <el-input v-model="ruleForm.specs" />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 生产日期 -->
      <el-form-item label="生产日期" prop="date">
        <el-input v-model="ruleForm.date" class="data" />
      </el-form-item>
      <!-- 销售容量 -->
      <el-form-item label="销售容量" prop="capacity">
        <el-input v-model="ruleForm.capacity" class="capacity" />
      </el-form-item>
      <!-- 销售区域 -->
      <el-form-item label="销售区域" prop="domain" class="spacing">
        <el-input v-model="ruleForm.domain" class="domain" />
      </el-form-item>

      <!-- 上传图片 -->
      <el-form-item label="商品主题：" prop="img">
        <div class="size">
          图片大小不能超过1MB；1:1以上的图片详情页自动提供放大功能。白底图用来展示，若是没有则取第二张图片
        </div>
        <el-upload
          v-model:file-list="fileList"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <el-icon>
            <p>+</p>
          </el-icon>
        </el-upload>

        <el-upload
          v-model:file-list="fileList"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <el-icon>
            <p>+</p>
          </el-icon>
        </el-upload>
      </el-form-item>

      <!-- 上传图片 -->
      <el-form-item label="商品详情：" prop="img">
        <div class="size">
          详情切图请按顺序上传，展示宽度为790px，高度不定，最多20张，每张不得大于2M。
        </div>
        <el-upload
          v-model:file-list="fileList"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <el-icon>
            <p>+</p>
          </el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button>取消</el-button>
        <el-button class="go">提交</el-button>
      </el-form-item>
    </el-form>
  </main>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";

import type { UploadProps, UploadUserFile } from "element-plus";

export default defineComponent({
  setup() {
    const formSize = ref("default");
    const ruleFormRef = ref<FormInstance>();
    const ruleForm = reactive({
      name: "",
      type: "",
      brand: "",
      production: "",
      amount: "",
      specs: "",
      date: "",
      capacity: "",
      domain: "",
    });

    const rules = reactive<FormRules>({
      name: [
        {
          required: true,
          message: "请输入商品名称",
          trigger: "blur",
        },
      ],
      type: [
        {
          required: true,
          message: "请输入商品类型",
          trigger: "change",
        },
      ],
      brand: [
        {
          required: true,
          message: "请输入品牌",
          trigger: "change",
        },
      ],
      production: [
        {
          required: true,
          message: "请输入产地",
          trigger: "change",
        },
      ],
      amount: [
        {
          required: true,
          message: "请输入需量",
          trigger: "change",
        },
      ],
      specs: [
        {
          required: true,
          message: "请输入规格",
          trigger: "change",
        },
      ],
      date: [
        {
          required: true,
          message: "请输入生产日期",
          trigger: "change",
        },
      ],
      capacity: [
        {
          required: true,
          message: "请输入容量",
          trigger: "blur",
        },
      ],
      domain: [
        {
          required: true,
          message: "请输入销售区域",
          trigger: "blur",
        },
      ],
      img: [
        {
          required: true,
        },
      ],
    });

    // 上传图片
    const fileList = ref<UploadUserFile[]>([]);

    const dialogImageUrl = ref("");
    const dialogVisible = ref(false);

    const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
      console.log(uploadFile, uploadFiles);
    };

    const handlePictureCardPreview: UploadProps["onPreview"] = (uploadFile) => {
      dialogImageUrl.value = uploadFile.url!;
      dialogVisible.value = true;
    };

    return {
      rules,
      ruleForm,
      formSize,
      ruleFormRef,
      handlePictureCardPreview,
      handleRemove,
      dialogImageUrl,
      dialogVisible,
      fileList,
    };
  },
});
</script>

<style lang="less" scoped>
main {
  width: 100%;
  min-width: 1120px;
  border-radius: 20px;
  background: #ffffff;
  padding-top: 16px;
  padding-bottom: 44px;
  position: relative;
  .h {
    background: #ffdb92;
    width: 160px;
    height: 50px;
    border-radius: 0 25px 25px 0;
    margin-top: 16px;
    box-shadow: 0px 2px 1px 0px rgba(0, 0, 0, 0.5);
    padding-left: 20px;
    line-height: 50px;
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 27px;
  }
  .title-x {
    padding: 32px 0 0 58px;
    font-weight: 400;
    height: 18px;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #333333;
  }
  .demo-ruleForm {
    margin: 0 auto;
    width: 500px;
  }
  .spacing {
    margin-bottom: 37px;
  }
  .title {
    position: absolute;
    left: 0px;
    top: 300px;
  }
  .data {
    width: 260px;
  }
  .capacity {
    width: 180px;
  }
  .introduce {
    position: absolute;
    left: 0;
    top: 470px;
  }
  /deep/.el-upload--picture-card {
    height: 80px;
    width: 80px;
  }
  /deep/.el-upload--picture-card {
    margin-left: 20px;
  }
  .size {
    height: 13px;
    font-size: 12px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #999999;
    line-height: 35px;
    white-space: nowrap;
    margin-bottom: 20px;
  }
  .go {
    background: #ffa79c;
    color: #ffffff;
  }
}
</style>
